import {
	md5
} from "@/utils/md5.js";
import {aes_encrypt} from '@/utils/aes.js'
import Cookies from 'js-cookie'
// 测试站后端端口 26613
// 正式站后端端口 25547

//guo
// const baseURL ="http://192.168.0.155:25547"

//xu
// const baseURL ="http://192.168.2.102:25547"

/////测试服务器
// const baseURL = "http://36.134.47.229:26613";

/////正式服务器
const baseURL = "http://58.59.15.12:8900";


const secretKey = "cqjy2023";


export default {
	baseURL,
	// get请求
	get(params) {
		return this.request(params, "GET");
	},
	// put请求
	put(params) {
		return this.request(params, "PUT");
	},
	// post请求
	post(params) {
		return this.request(params, "POST");
	},
	// delete请求
	delete(params) {
		return this.request(params, "DELETE");
	},
	upload(params) {
		let url = baseURL + "/app/projectUserInfo/projectReportFileUpload";
		let timeStamp = (new Date()).valueOf();
		let header = {
			token: aes_encrypt(timeStamp + Cookies.get('token')),
			signtime: timeStamp,
			sign: md5(secretKey + timeStamp),
		};
		
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: url,
				filePath: params.url,
				name: "file",
				header: header,
				formData: params.data,
				success: (res) => {
					let data = JSON.parse(res.data);
					// 请求成功后的操作;
					if (res.statusCode == 200) {
						resolve(data);
					} else if (res.data.message == "Token失效，请重新登录") {
						uni.clearStorageSync();
						// console.log(res.statusCode, "报错地址", baseURL + url)
						// uni.clearStorageSync()
						// var page = getCurrentPages()
						// if (page[0].route == 'pages/mine/index') {
						// 	page[0].onShow()
						// }
						// uni.reLaunch({
						//   url: "/pages/login/login?",
						// });
					} else {
						//其他异常
						reject("运行时错误,请稍后再试");
						uni.showToast({
							title: "服务器连接错误",
							icon: "none",
							duration: 2000,
						});
					}
				},
			});
		});
	},
	segmentUpload(params) {
		let url = baseURL + "sys/common/segmentUpload";
		let header = {
			"X-Access-Token": Cookies.get('token') ?
				Cookies.get('token') :
				"",
		};
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: url,
				name: "file",
				header: header,
				formData: params,
				success: (res) => {
					let data = JSON.parse(res.data);
					// 请求成功后的操作;
					if (res.statusCode == 200) {
						resolve(data);
					} else if (res.data.message == "Token失效，请重新登录") {
						uni.clearStorageSync();
					} else {
						//其他异常
						reject("运行时错误,请稍后再试");
						uni.showToast({
							title: "服务器连接错误",
							icon: "none",
							duration: 2000,
						});
					}
				},
			});
		});
	},
	request(params, method) {
		// 请求地址
		let url = baseURL + params.url;
		let date = new Date();
		let timeStamp = (new Date()).valueOf();
		// 请求头
		let header = {
			"content-type": params.contentType ?
				params.contentType :
				"application/json",
			token: aes_encrypt(timeStamp + Cookies.get('token')),
			signtime: timeStamp,
			sign: md5(secretKey + timeStamp),
		};
		// 请求方式
		method = method;
		let data = params.data; // 请求参数
		return new Promise((resolve, reject) => {
			// 发起请求
			uni.request({
				url,
				method,
				header,
				data,
				success(res) {
					console.log('res',res)
					// 请求成功后的操作
					if (res.statusCode == 200) {
						resolve(res.data);
					} else if (res.data.message == "Token失效，请重新登录") {
						let loginInfo = uni.getStorageSync("loginInfo");
						uni.clearStorageSync();
						uni.setStorageSync("loginInfo", loginInfo);
						// console.log(res.statusCode, "报错地址", baseURL + url)
						// uni.clearStorageSync()
						// var page = getCurrentPages()
						// if (page[0].route == 'pages/mine/index') {
						// 	page[0].onShow()
						// }
						uni.reLaunch({
							url: "/pages/login/Login",
						});
					} else {
						console.error("报错地址", url);

						//其他异常
						reject("运行时错误,请稍后再试");
						uni.showToast({
							title: "服务器连接错误",
							icon: "none",
							duration: 2000,
						});
					}
				},
				fail(err) {
					reject(err);
				},
			});
		});
	},
};